<?php

/**
 * Created by PhpStorm.
 * User: Apple
 * Date: 2018/11/1 0001
 * Time: 14:42
 */

namespace App\WebSocket;

use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\Socket\AbstractInterface\Controller;


/**
 * Class Index
 *
 * 此类是默认的 websocket 消息解析后访问的 控制器
 *
 * @package App\WebSocket
 */
class Index extends Controller
{
    function hello()

    {
//        $this->response()->setMessage('call hello with arg:' . json_encode());

        $server = ServerManager::getInstance()->getSwooleServer();
        $start_fd = 0;
         $content = $this->caller()->getArgs();
        $content = json_encode($content);
        while(true)
        {
            $conn_list = $server->getClientList($start_fd, 10);
            if ($conn_list===false or count($conn_list) === 0)
            {
                break;
            }
            $start_fd = end($conn_list);
            foreach($conn_list as $fd)
            {

                $server->push($fd, $content);
            }
        }

    }
    public  function userId(){

        $hId= $this->caller()->getClient()->getFd();
        $server = ServerManager::getInstance()->getSwooleServer();
        $start_fd = 0;
        $content = $this->caller()->getArgs();
        while(true)
        {
            $conn_list = $server->getClientList($start_fd, 10);
            if ($conn_list===false or count($conn_list) === 0)
            {
                break;
            }
            $start_fd = end($conn_list);

            foreach($conn_list as $fd)
            {
                if(!is_array($content)){
                    $content = json_decode($content,true);
            }   $content['idArr'] = $conn_list;
                $content['uId'] = $fd;
                $content['hId'] = $hId;
                $content = json_encode($content);
                $server->push($fd, $content);
            }

        }


    }

    public function who()
    {
        $this->response()->setMessage('your fd is ' . $this->caller()->getClient()->getFd());
    }

    public  function Gskill(){
        $server = ServerManager::getInstance()->getSwooleServer();
        $start_fd = 0;
        $content = $this->caller()->getArgs();
        while(true)
        {
            $conn_list = $server->getClientList($start_fd, 10);
            if ($conn_list===false or count($conn_list) === 0)
            {
                break;
            }
            $start_fd = end($conn_list);

            foreach($conn_list as $fd)
            {
                if(!is_array($content)){
                    $content = json_decode($content,true);
                }   $content['idArr'] = $conn_list;
                $content = json_encode($content);
                $server->push($fd, $content);
            }
        }
    }

}